package com.seleniti.enpareja.web.desktop.client.control.navigation;

import org.ioc.commons.flowcontrol.activitymapper.ActivityProvider;
import org.ioc.commons.impl.gwt.client.flowcontrol.activitymapper.AppActivity;
import org.ioc.commons.impl.gwt.client.flowcontrol.activitymapper.GoPlace;
import org.ioc.commons.impl.gwt.client.flowcontrol.activitymapper.GoPlaceActivityMapper;

import com.seleniti.enpareja.controller.control.factory.ClientFactory;
import com.seleniti.enpareja.controller.control.place.EnParejaPlace;
import com.seleniti.enpareja.controller.ui.view.AdminConsoleView;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsView;
import com.seleniti.enpareja.controller.ui.view.GetCoupleView;
import com.seleniti.enpareja.controller.ui.view.GettingStartedView;
import com.seleniti.enpareja.controller.ui.view.HomeTasksView;
import com.seleniti.enpareja.controller.ui.view.LoginView;
import com.seleniti.enpareja.controller.ui.view.MainView;
import com.seleniti.enpareja.controller.ui.view.MealsView;
import com.seleniti.enpareja.controller.ui.view.PendingShoppingView;
import com.seleniti.enpareja.controller.ui.view.PreDownloadAppWarningView;
import com.seleniti.enpareja.controller.ui.view.RequiredReloadView;
import com.seleniti.enpareja.controller.ui.view.SharedExpensesView;
import com.seleniti.enpareja.controller.ui.view.ShoppingListView;
import com.seleniti.enpareja.controller.ui.view.SignUpView;
import com.seleniti.enpareja.controller.ui.view.SolveNoPasswordSetView;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView;
import com.seleniti.enpareja.controller.ui.view.WorkShiftsView;
import com.seleniti.enpareja.controller.ui.widget.SliderPanel.BoardType;
import com.seleniti.enpareja.web.appbase.client.control.navigation.LoginPlace;
import com.seleniti.enpareja.web.appbase.client.control.navigation.SettingsPlace;
import com.seleniti.enpareja.web.desktop.client.control.activity.DesktopGenericActivity;
import com.seleniti.enpareja.web.desktop.client.ui.view.AdminConsoleViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.CoupleRequestsViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.GetCoupleViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.GettingStartedViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.HomeTasksViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.LoginViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.MainViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.MealsViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.PendingShoppingViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.PreDownloadAppWarningViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.RequiredReloadViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.SettingsViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.SharedExpensesViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.ShoppingListViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.SignUpViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.SolveNoPasswordSetViewWidget;
import com.seleniti.enpareja.web.desktop.client.ui.view.WorkShiftsViewWidget;

public class DesktopActivityMapper extends GoPlaceActivityMapper<EnParejaPlace> implements
		ActivityProvider<EnParejaPlace, AppActivity<?>> {

//	private static final FormatterLogger logger = FormatterLogger.getLogger(DesktopActivityMapper.class);
	private ClientFactory clientFactory;

	public DesktopActivityMapper(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.registerActivityProvider(this);
	}

	@SuppressWarnings("incomplete-switch")
	public DesktopGenericActivity<?> getNewActivityInstance(EnParejaPlace placeId) {
		DesktopGenericActivity<?> activity = null;
		switch (placeId) {
		case LoginPlace:
			activity = new DesktopGenericActivity<LoginView>(new LoginViewWidget(), false);
			break;
		case Main:
			activity = new DesktopGenericActivity<MainView>(new MainViewWidget(), true);
			break;
		case SignUp:
			activity = new DesktopGenericActivity<SignUpView>(new SignUpViewWidget(), false);
			break;
		case UserSettings:
			activity = new DesktopGenericActivity<UserSettingsView>(new SettingsViewWidget(), true);
			break;
		case CoupleRequests:
			activity = new DesktopGenericActivity<CoupleRequestsView>(new CoupleRequestsViewWidget(), true);
			break;
		case GetCouple:
			activity = new DesktopGenericActivity<GetCoupleView>(new GetCoupleViewWidget(), true);
			break;
		case AdminConsole:
			activity = new DesktopGenericActivity<AdminConsoleView>(new AdminConsoleViewWidget(), true);
			break;
		case ShoppingList:
			activity = new DesktopGenericActivity<ShoppingListView>(new ShoppingListViewWidget(), true);
			break;
		case HomeTasks:
			activity = new DesktopGenericActivity<HomeTasksView>(new HomeTasksViewWidget(), true);
			break;
		case Meals:
			activity = new DesktopGenericActivity<MealsView>(new MealsViewWidget(), true);
			break;
		case WorkShifts:
			activity = new DesktopGenericActivity<WorkShiftsView>(new WorkShiftsViewWidget(), true);
			break;
		case SharedExpenses:
			activity = new DesktopGenericActivity<SharedExpensesView>(new SharedExpensesViewWidget(), true);
			break;
		case PendingShopping:
			activity = new DesktopGenericActivity<PendingShoppingView>(new PendingShoppingViewWidget(), true);
			break;
		case GettingStarted:
			activity = new DesktopGenericActivity<GettingStartedView>(new GettingStartedViewWidget(), false);
			break;
		case PreDownloadAppWarning:
			activity = new DesktopGenericActivity<PreDownloadAppWarningView>(new PreDownloadAppWarningViewWidget(), false);
			break;
		case RequiredReload:
			activity = new DesktopGenericActivity<RequiredReloadView>(new RequiredReloadViewWidget(), false);
			break;
		case SolveNoPasswordSet:
			activity = new DesktopGenericActivity<SolveNoPasswordSetView>(new SolveNoPasswordSetViewWidget(), false);
			break;
		}
		return activity;
	}

	@Override
	protected void processActivityFound(GoPlace<EnParejaPlace> goPlace, AppActivity<?> activity) {

		if (activity != null) {
			((DesktopGenericActivity<?>) activity).setClientFactory(clientFactory);
		}

		EnParejaPlace placeId = goPlace.getPlaceId();

		// String subPlaceToken = goPlace.getSubPlaceToken();

		switch (placeId) {
		case LoginPlace:
			if (goPlace instanceof LoginPlace && activity.getView() instanceof LoginView) {
				String userEmail = ((LoginPlace) goPlace).getUserEmail();
				EnParejaPlace goWhenOk = ((LoginPlace) goPlace).getPlaceWhenLoginOk(); 
				if (userEmail != null) {
					((LoginView) activity.getView()).loginName().setValue(userEmail);
				}
				
				((LoginView) activity.getView()).setGoToPlaceWhenOk(goWhenOk);
			}

		case Main:
		case SignUp:
			clientFactory.getShell().mainContainer().setBoardType(BoardType.WITH_LOGO);
			break;
		case UserSettings:
			if (goPlace instanceof SettingsPlace && activity.getView() instanceof UserSettingsView) {
				boolean passwordChangeEnabled = ((SettingsPlace) goPlace).isPasswordChangeEnabled();
				boolean showDownloadAppWhenPassword = ((SettingsPlace) goPlace).isShowDownloadAppWhenPassword();
				
				((UserSettingsView) activity.getView()).setPasswordChangeEnabled(passwordChangeEnabled);
				((UserSettingsView) activity.getView()).setShowDownloadAppWhenPassword(showDownloadAppWhenPassword);
			}
		default:
			clientFactory.getShell().mainContainer().setBoardType(BoardType.WITHOUT_LOGO);

		}
	}
}
